package com.example.demo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
    private static final String TAG="MainActivity.this";
    private Button btn1;
    private Button btn2;
    private ImageView iv_img;
    private Handler handler;
    private int GET_IMG;
    private int GET_PHONE;
    private EditText edtPhone;
    private TextView txtView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1=findViewById(R.id.btn_1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String phone=edtPhone.getText().toString();
//                        useHttpUrlConnectionGet(phone);
                        useHttpUrlConnectionGet();

                    }
                }).start();
            }
        });
        btn2=findViewById(R.id.btn_2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String phone=edtPhone.getText().toString();
                        useHttpUrlConnectionPost(phone);
                    }
                }).start();
            }
        });
        iv_img=findViewById(R.id.iv_img);
        edtPhone=findViewById(R.id.edt_phone);
        txtView=findViewById(R.id.tv_text);
        handler=new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if(msg.what==GET_IMG){
//                    图片
                    Bitmap bitmap=(Bitmap)msg.obj;
                    iv_img.setImageBitmap(bitmap);
//                    网页
                  /*  String info=(String)msg.obj;
                    txtView.setText(info);*/
                    return true;
                }
                return false;
            }
        });
    }
    private void useHttpUrlConnectionPost(String phone) {
        String phoneBaseUrl="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
        try {
            URL url=new URL(phoneBaseUrl);
            HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            String body="mobileCode="+phone+"&userID=";
            OutputStream outputStream=httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(outputStream,"utf-8"));
            bufferedWriter.write(body);
            bufferedWriter.flush();
            bufferedWriter.close();
            InputStream inputStream=httpURLConnection.getInputStream();
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
            StringBuffer stringBuffer=new StringBuffer();
            String line =bufferedReader.readLine();
            while(line!=null){
                stringBuffer.append(line);
                line=bufferedReader.readLine();
            }
            Log.e(TAG,"stringBuffer ="+stringBuffer.toString());
            Message message=Message.obtain();
            message.what=GET_PHONE;
            message.obj=stringBuffer.toString();
            handler.sendMessage(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void useHttpUrlConnectionGet() {
//    private void useHttpUrlConnectionGet(String phone) {
//        String phoneBaseUrl="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
//        String strUrl=phoneBaseUrl.concat("?mobileCode=").concat(phone).concat("&userID=");
//        网页
//        String strUrl="http://ica.gdcp.cn/web/loadWebPage.do?1_649_44015038150_3";
//        图片
  String strUrl="http://lib.gdcp.cn/UploadFile/916/2017/9/6/201796165316396.jpg";
        try {
            URL url=new URL(strUrl);
            HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
            int code=httpURLConnection.getResponseCode();
            Log.e(TAG,"code= "+code);

//显示图片
            Bitmap bitmap= BitmapFactory.decodeStream(httpURLConnection.getInputStream());
            Message message=Message.obtain();
            message.what=GET_IMG;
            message.obj=bitmap;
            handler.sendMessage(message);
//显示页面代码
//            InputStream inputStream=httpURLConnection.getInputStream();
//            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
//            StringBuffer stringBuffer=new StringBuffer();
//            String line =bufferedReader.readLine();
//            while(line!=null){
//                stringBuffer.append(line);
//                line=bufferedReader.readLine();
//            }
//            Log.e(TAG,"stringBuffer ="+stringBuffer.toString());
//            Message message=Message.obtain();
//            message.what=GET_PHONE;
//            message.obj=stringBuffer.toString();
//            handler.sendMessage(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
